package com.infinitus.nginxInfo.service.util;



import java.security.Key;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
* <p>
* <b>AES</b> AES加解密
* </p>
* 
 * @author rainsoft
* @version $Revision: 3807 $ $Date: 2012-11-21 15:31:51 +0800 (周三, 21 十一月 2012) $
*/

public class AES {

    private static Symmetric symmetric;

    private static String keyalg = "AES";
    
    private static String algorithm = "AES";

    protected static Log LOG = LogFactory.getLog(AES.class);

    static {
        symmetric = new Symmetric(keyalg, algorithm, "ECB", "PKCS7Padding", 128);
    }

    /**
    * 生成密钥字串
    * 
     * @return
    */
    public static String generateKeyStr() {
        try {
            byte[] key = symmetric.generateKey().getEncoded();
            return Base64.encode(key);
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    /**
    * 生成字节密钥
    * 
     * @return
    */
    public static byte[] generateKey() {
        try {
            return symmetric.generateKey().getEncoded();
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    /**
    * 加密字串，字串用UTF-8生成字节
    * 
     * @param plaintext
    * @param key
    * @return
    */
    public static String encrypt(String plaintext, String key) {
        try {
            return symmetric.encrypt(plaintext, str2key(key));
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    public static byte[] encrypt(byte[] plaintext, byte[] key) {
        try {
            return symmetric.encrypt(plaintext, byte2key(key));
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    /**
    * 解密字串，用UTF-8生成字串
    * 
     * @param cryptotext
    * @param key
    * @return
    */
    public static String decrypt(String cryptotext, String key) {
        try {
            return symmetric.decrypt(cryptotext, str2key(key));
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    public static byte[] decrypt(byte[] cryptotext, byte[] key) {
        try {
            return symmetric.decrypt(cryptotext, byte2key(key));
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    /**
    * 将字串转换为KEY
    * 
     * @param key
    * @return
    */
    public static Key str2key(String skey) {
        try {
            return byte2key(Base64.decode(skey));
        } catch (Exception e) {
            LOG.debug(e);
        }
        return null;
    }

    public static Key byte2key(byte[] bkey) {
        return new SecretKeySpec(bkey, keyalg);
    }

    public static void main(String[] args) {
    	/*
        String str1 = "sss";
    	System.out.println(com.alibaba.fastjson.JSON.parseObject(str1).getString("userName"));
        if(true)return;
        */
        try {
//            String key = generateKeyStr();
            //String key = "01tkV5k3IBLom6LSEhQbzw==";
//            System.out.println("密钥:" + key);
//            String str = "{\"userName\":\"330897159\",\"userRealName\":\"黄健\",\"orgName\":\"广东分公司\"}";
//            System.out.println("明文:" + str);
//            String cryptotext = encrypt(str, key);
//            System.out.println("密文:" + cryptotext);
//            String plaintext = new String(Base64.decode("SHdnKy9KMW1KZXBoQ0xEWW5HUFZ2OXhsbDhCeWpDeFZpeUdVVDRGTUM0QkhrUjRhak9RMDdQY2ZuVDdaak8zTSMjMzY2MTE2MDAw"));
//            System.out.println(plaintext);
//            
//            
//            System.out.println("解密:" + decrypt("R+IY996wdnIKmDIYCQjKwlFQbL0gpZtcScrOnOjKWybVxMgCCQccbxEWXtP/MaDXYJTWRdLf5goP3WxJlWA12TYkJeUOcUhIAE7Nb6eWJBw=","01tkV5k3IBLom6LSEhQbzw=="));
//            
            
//            Pattern tkPtn = Pattern.compile("(\\?|\\&)access_token=([^\\&]+)");
//			Matcher tkMec = tkPtn.matcher("/api/common/users/policy/logininfo?PoEntryClassEnum=M&appVersion=2.0.23&brand=oppo&coreVersion=3.18.24-perf-gdff7a86root%40ubuntu-121-140))1SMPPREEMPTSatJun312%3A58%3A05CST2017&deviceId=F427A5B73AA408EF&groupCacheTime=1498434100720&imei=F427A5B73AA408EF&machineModel=OPPOR9sk&model=2&netType=wifi&os=2&osVersion=6.0.1&screen=1080x1920&access_token=SHdnKy9KMW1KZXBoQ0xEWW5HUFZ2OXhsbDhCeWpDeFZpeUdVVDRGTUM0QkhrUjRhak9RMDdQY2ZuVDdaak8zTSMjMzY2MTE2MDAw&app_key=gbss-bupm&timestamp=1498637444755&sign=09936458D5C70FFB3330275BE882843D");
//			if(tkMec.find()){
//				String decStr = new String(Base64.decode(tkMec.group(2)));
//				Pattern usPtn = Pattern.compile("#+(\\d+)");
//				Matcher usMec = usPtn.matcher(decStr);
//				if(usMec.find()){
//					String userId =usMec.group(1);
//					System.out.println(userId);
//				}
//			}
        	String ident = "\"\\x22MaQOSX47CT3eQ7+BwA8RYtFdkzzVYt4D4k1zZQqnc0+hRPVEpsc5iJStRsVt8Fx0NHGeTz89iCncBSpI0xxB4BQ/LCRFRJfLXihpZ1RF4S8=\\x22\"";
        	System.out.println(ident);
        	ident = ident.replaceAll("\"?\\\\x22\"?", "");
        	System.out.println(ident);
			String json = AES.decrypt(ident, "01tkV5k3IBLom6LSEhQbzw==");
			System.out.println(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

